package ds.tree;

public class BoundaryTraversal {

	private static class TreeNode {
		int value;
		TreeNode left = null;
		TreeNode right = null;

		TreeNode(int val) {
			this.value = val;
		}

		public String toString() {
			return String.valueOf(value);
		}
	}

	public void printLeaves(TreeNode node) {
		if (node == null) {
			return;
		}
		printLeaves(node.left);
		if (node.left == null && node.right == null) {
			System.out.print(node + " ");
		}
		printLeaves(node.right);
	}

	public void printLeft(TreeNode node) {
		if (node != null && node.left != null) {
			System.out.print(node + " ");
			printLeft(node.left);
		}
	}

	public void printRight(TreeNode node) {
		if (node != null && node.right != null) {
			System.out.print(node + " ");
			printRight(node.right);
		}
	}

	private static TreeNode tree() {
		TreeNode A = new TreeNode(1);
		TreeNode B = new TreeNode(2);
		TreeNode C = new TreeNode(3);
		TreeNode D = new TreeNode(4);
		TreeNode E = new TreeNode(5);
		TreeNode F = new TreeNode(6);
		TreeNode G = new TreeNode(7);

		A.left = B;
		A.right = C;
		B.left = D;
		B.right = E;
		C.left = F;
		C.right = G;

		return A;
	}

	public static void main(String[] args) {
		BoundaryTraversal boundary = new BoundaryTraversal();
		TreeNode node = tree();
		boundary.printLeft(node);
		boundary.printLeaves(node);
		boundary.printRight(node.right);
	}

}
